﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicketReservationSystemDataLayer
{
    public class EventDAO
    {
        public static IQueryable<Event> GetAllEvents(
            TicketReservationSystemDataContext context)
        {
            var events =
                from e in context.Events
                select e;
            return events;
        }

        public static IQueryable<Event> GetAllUpcomingEvents(
            TicketReservationSystemDataContext context)
        {
            var events =
                from e in context.Events
                where e.DateTime.AddDays(-3) >= DateTime.Now
                orderby e.DateTime
                select e;
            return events;
        }

        public static IQueryable<Event> GetAllUpcomingEventsFromCategory(
            TicketReservationSystemDataContext context, int categoryId)
        {
            var events =
                from e in context.Events
                where e.DateTime.AddDays(-3) >= DateTime.Now
                      && e.CategoryId == categoryId
                select e;
            return events;
        }

        public static bool CreateEvent(
            TicketReservationSystemDataContext context,
            string name, int locationId, int categoryId,
            DateTime dateTime, decimal price)
        {
            try
            {
                Event newEvent = new Event();
                newEvent.Name = name;
                newEvent.Price = price;
                newEvent.LocationId = locationId;
                newEvent.CategoryId = categoryId;
                newEvent.DateTime = dateTime;
                context.Events.InsertOnSubmit(newEvent);
                context.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool ModifyEvent(
            TicketReservationSystemDataContext context, Event oldEvent,
            string newName, int newLocationId, int newCategoryId, 
            decimal newPrice, DateTime newTime)
        {
            try
            {
                oldEvent.Name = newName;
                oldEvent.LocationId = newLocationId;
                oldEvent.Price = newPrice;
                oldEvent.CategoryId = newCategoryId;
                oldEvent.DateTime = newTime;
                context.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool DeleteEventById(
            TicketReservationSystemDataContext context, int eventId)
        {
            try
            {
                context.Events.DeleteOnSubmit(
                    FindEventById(context, eventId).First());
                context.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static IQueryable<Event> FindEventById(
            TicketReservationSystemDataContext context, int eventId)
        {
             var currentEvent=
                from e in context.Events
                where e.Id == eventId
                select e;
            return currentEvent;
        }
    }
}
